package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.mdd.admin.LikeAdminThreadLocal;
import com.mdd.admin.service.IIndexService;
import com.mdd.admin.service.IOrderService;
import com.mdd.admin.vo.system.SystemConsoleVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.mapper.orders.OrdersDishMapper;
import com.mdd.common.mapper.orders.OrdersMapper;
import com.mdd.common.util.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * 主页服务实现类
 */
@Service
public class IndexServiceImpl implements IIndexService {

    @Resource
    private IOrderService iOrderService;
    @Resource
    private OrdersMapper ordersMapper;
    @Resource
    private OrdersDishMapper ordersDishMapper;

    /**
     * 控制台数据
     *
     * @author fzr
     * @return Map<String, Object>
     */
    @Override
    public Map<String, Object> console(SystemConsoleVo vo) {
        Map<String, Object> console = new LinkedHashMap<>();

        // 版本信息
        Map<String, Object> version = new LinkedHashMap<>();
        version.put("name", ConfigUtils.get("website", "name", "LikeAdmin-Java"));
        version.put("version", GlobalConfig.version);
        version.put("website", "www.likeadmin.cn");
        version.put("based", "Vue3.x、ElementUI、MySQL");
        Map<String, String> channel = new LinkedHashMap<>();
        channel.put("gitee", "https://gitee.com/likeadmin/likeadmin_java");
        channel.put("website", "https://www.likeadmin.cn");
        version.put("channel", channel);
        console.put("version", version);

        // 今日数据
        Map<String, Object> today = new LinkedHashMap<>();
        console.put("today", today);

        Integer adminId = LikeAdminThreadLocal.getAdminId();
        Map<String,String> orderStatisticsInfo=iOrderService.getOrderStatistics(adminId,vo.getCheckoutTimeStr(),vo.getChildIdList());
        // 总销售额
        today.put("todaySales", orderStatisticsInfo.get("amountCount")==null?"0":orderStatisticsInfo.get("amountCount"));
        // 订单量
        today.put("todayOrder", orderStatisticsInfo.get("orderNum")==null?"0":orderStatisticsInfo.get("orderNum"));
        // 订单量
        today.put("todayVisits", orderStatisticsInfo.get("userNumCount")==null?"0":orderStatisticsInfo.get("userNumCount"));
        // 访客图表
        Map<String, Object> visitor = new LinkedHashMap<>();
        visitor.put("date", TimeUtils.daysAgoDate(15));
        List<String> datestrings = TimeUtils.daysAgoDate(15);
        // 查詢某些日期的銷售額
        // 查询当前商家子商家
        List<Integer> currentAidChindId =new ArrayList<>();
        if(vo.getChildIdList()!=null&&vo.getChildIdList().size()>0){
            adminId =null;
            currentAidChindId=vo.getChildIdList();
        }else{
            currentAidChindId = ordersMapper.getCurrentAidChindId("," + adminId + ",");
        }
        List<Map<String,Object>> getOrderStatisticsByDate=ordersDishMapper.getOrderStatisticsByDate(adminId,currentAidChindId,datestrings);
        Map<String,Integer> dateAmountMap=new HashMap<>();
        for(Map<String,Object> map:getOrderStatisticsByDate){
            String time = map.get("dateColumn").toString();
            Double amountCount = (Double) map.get("amountCount");
            dateAmountMap.put(time,amountCount.intValue());
        }

        List<Integer> amount=new ArrayList<>();
        if(CollectionUtils.isNotEmpty(datestrings)){
            for (String dateTime : datestrings) {
                Integer i = dateAmountMap.get(dateTime);
                if(i!=null){
                    amount.add(i);
                }else{
                    amount.add(0);
                }
            }
            visitor.put("list", amount);
        }else{
            visitor.put("list", Arrays.asList(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0));
        }

        // 查询当前商家子商家
        List<Long> time;
        if(vo.getCheckoutTimeStr()!=null){
            time = new ArrayList<>();
            List<String> list = Arrays.asList(vo.getCheckoutTimeStr().split("[,]"));
            list.forEach(timeStr->{
                Long l = Long.parseLong(timeStr);
                time.add(l);
            });
        }else{
            time = TimeUtils.getLongTime();
        }
        List<Map<String,Object>> ordersDishRes= ordersDishMapper.getUpDishInfo(adminId,currentAidChindId,time.get(0),time.get(1));
        if(ordersDishRes.size()>0){
            // 有被点菜品
            List<String> disheTitles=new ArrayList<>();
            List<String> couList=new ArrayList<>();
            for(Map<String,Object> map:ordersDishRes){
                String title = map.get("title").toString();
                disheTitles.add(title);
                String cou = map.get("cou").toString();
                couList.add(cou);
            }
            visitor.put("ordersDishTileRes", disheTitles);
            visitor.put("ordersDishCountRes", couList);
        }


        console.put("visitor", visitor);

        return console;
    }

    /**
     * 公共配置
     *
     * @author fzr
     * @return Map<String, Object>
     */
    @Override
    public Map<String, Object> config() {
        Map<String, String> website   = ConfigUtils.get("website");
        String copyright = ConfigUtils.get("website", "copyright", "");

        String captchaStatus = YmlUtils.get("like.captcha.status");

        Map<String, Object> map = new LinkedHashMap<>();
        map.put("loginCaptcha", StringUtils.isNotNull(captchaStatus) && captchaStatus.equals("true"));
        map.put("webName", website.getOrDefault("name", ""));
        map.put("webLogo", RequestUtils.uri()+website.getOrDefault("logo", ""));
        map.put("webFavicon",RequestUtils.uri()+website.getOrDefault("favicon", ""));
        map.put("webBackdrop",RequestUtils.uri()+website.getOrDefault("backdrop", ""));;
        map.put("ossDomain", UrlUtils.domain());
        map.put("copyright", ArrayUtils.stringToListAsMapStr(copyright));

        return map;
    }

}
